package ar.unq.edu.agenda.home;

import java.text.ParseException;

import org.apache.commons.collections.Predicate;

import ar.unq.edu.agenda.domain.City;
import ar.unq.edu.agenda.domain.Contact;
import ar.unq.edu.agenda.domain.ContactExample;
import ar.unq.edu.agenda.utils.DateHelper;
import ar.unq.edu.agenda.utils.Validator;

import org.uqbar.commons.model.CollectionBasedHome;

public class ContactHome extends CollectionBasedHome<Contact> {


	public ContactHome()
	{
		
		try {
			this.create(new Contact("Juan", "Perez", "12345678", "jperez@mail.com", DateHelper.fromStringToDate("2000/01/01"), "Comentarios!", new City("La Plata")));
			this.create(new Contact("Jorge", "Gomez", "12345678", "jgomez@mail.com", DateHelper.fromStringToDate("2000/01/01"), "Comentarios!", new City("Quilmes")));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@Override
	public Class<Contact> getEntityType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Contact createExample() {
		return new ContactExample();
	}

	@Override
	public Predicate getCriterio(final Contact example) {
		return new Predicate() {
			@Override
			public boolean evaluate(Object arg) {
				Contact contact = (Contact) arg; 
				boolean output = (Validator.matchWithString(contact.getName(),example.getName()) 
							&& Validator.matchWithString(contact.getLastName(),example.getLastName())
							&& Validator.matchWithString(contact.getEmail(), example.getEmail())
							&& Validator.matchWithString(contact.getCity().getName(), example.getCity().getName()));
				return output;
			}
		};
		
	}
	
	
	/**
	 * Obtiene un Predicate con aquellos Contactos cuyo nombre sea igual al nombre del example
	 * @param example Contacto que posee la condición de búsqueda 
	 * @return un Predicate
	 */
	public Predicate getByName(final Contact example)
	{
		return new Predicate() {
			@Override
			public boolean evaluate(Object arg) {
				Contact contact = (Contact) arg;
				return contact.getName().toLowerCase().contains(example.getName().toLowerCase());
			}
		};
	}
	
	/**
	 * Obtiene un Predicate con aquellos Contactos cuyo Apellido sea igual al nombre del example
	 * @param example Contacto que posee la condición de búsqueda 
	 * @return un Predicate
	 */
	public Predicate getByLastName(final Contact example)
	{
		return new Predicate() {
			@Override
			public boolean evaluate(Object arg) {
				Contact contact = (Contact) arg;
				return contact.getLastName().toLowerCase().contains(example.getLastName().toLowerCase());
			}
		};
	}	
	


	
	


}
